package com.atguigu.scheduler.job;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * Quartz调度任务默认保存到内存，可以持久化到mysql
 *
 */
public class SimpleJob implements Job {

    public SimpleJob() {
    }

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        System.out.println("任务被触发执行！");
    }



    public static void main(String[] args) throws SchedulerException {

        //Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        StdSchedulerFactory factory = new StdSchedulerFactory();
        //实例化调度器
        Scheduler scheduler = factory.getScheduler();
        //调度器启动
        scheduler.start();

        //定义任务
        JobDetail job = newJob(SimpleJob.class)
                .withIdentity("myJob", "group1")
                .usingJobData("JobDetail","JobDetail存储的value")
                .build();


        JobDataMap jobDataMap = job.getJobDataMap();
        String name = job.getKey().getName();
        String group = job.getKey().getGroup();
        String jobName = job.getJobClass().getName();

        //定义触发器
        SimpleTrigger trigger = newTrigger()
                .withIdentity("myTrigger", "group1")
                .usingJobData("trigger","trigger存储的value")
                .startNow()
                .withSchedule(simpleSchedule()
                        .withIntervalInSeconds(5)
                        .repeatForever())
                .build();

        scheduler.scheduleJob(job, trigger);
    }
}
